<?php

require_once('query_snap.php');


function    usage($argv)
{
    printf("\tUSAGE : %s [ --gate tcp://127.0.0.1:14390 ] --urlmd5 0123456789ABCDEF [ --encoding utf-8 ]\n", $argv[0]);
}


class   Parameter
{
    public $gate = 'tcp://127.0.0.1:14390';
    public $urlmd5 = False;
    public $encoding = False;
}

function    parseArguments($argc, $argv)
{
    $parameter = new Parameter();

    for ($i=0; $i < $argc; $i++) {
        if (0 == strcmp('--gate', $argv[$i])) {
            if ($i < ($argc - 1)) {
                $parameter->gate = $argv[ $i + 1 ];
            }
        }
        elseif (0 == strcmp('--urlmd5', $argv[$i])) {
            if ($i < ($argc - 1)) {
                $parameter->urlmd5 = $argv[ $i + 1 ];
            }
        }
        elseif (0 == strcmp('--encoding', $argv[$i])) {
            if ($i < ($argc - 1)) {
                $parameter->encoding = tolower($argv[ $i + 1 ]);
            }
        }
    }

    return $parameter;
}


// parse parameters
$parameter = parseArguments($argc, $argv);
if (empty($parameter->urlmd5) || empty($parameter->gate)) {
    usage($argv);
    exit(1);
}

$result = query_snap($parameter->gate, $parameter->urlmd5);
if (empty($result)) {
    echo 'query urlmd5 : ',$parameter->urlmd5,"\n";
    echo '      result : EMPTY.',"\n";
    exit(0);
}

if ($parameter->encoding && ('gbk' != $parameter->encoding)) {
    $result = iconv('gbk', $parameter->encoding . '//IGNORE', $result);
}
echo $result,"\n";

exit(0);

